package com.tianya.common.taglib;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import com.tianya.common.util.Utility;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

public class WriteTimeLabelTag extends SimpleTagSupport {

	private String inputFormat;
	private String outputFormat;
	private boolean isSys = false;
	private boolean fullTime = false;
	private String value;


	public void setInputFormat(String inputFormat) {
		this.inputFormat = inputFormat;
	}

	public void setOutputFormat(String outputFormat) {
		this.outputFormat = outputFormat;
	}

	public void doTag() throws JspException, IOException {
		//yyyyMMdd
		String dateString = Utility.dateToString(new Date());
		if(!Utility.isEmpty(value)){
			value = value.replace(":", "");
			value = Utility.padLeft(value, "0", 4);
		}
		if (StringUtils.isEmpty(inputFormat)) {
			//inputFormat = Utility.getTimeInputFormat(isSys);
			inputFormat = fullTime? "HHmmss":"HHmm";
			
		}
//		if (StringUtils.isEmpty(outputFormat)) {
//			outputFormat = fullTime? Utility.getFullTimeFormat(isSys): Utility.getTimeFormat(isSys);
//		}
		if(Utility.isEmpty(value)){
			this.getJspContext().getOut().write("");
			return;
		}
		
		try {
			Date date = DateUtils
					.parseDate(dateString+value, new String[] { "yyyyMMdd"+inputFormat });
			SimpleDateFormat formater = new SimpleDateFormat( "yyyyMMdd"+outputFormat);   
			if(Utility.isEmpty(value)){
				this.getJspContext().getOut().write("");
			}else{
				this.getJspContext().getOut().write(formater.format(date).substring(8));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public void setValue(String value) {
		this.value = value;
	}

	public boolean getIsSys() {
		return isSys;
	}

	public void setIsSys(boolean isSys) {
		this.isSys = isSys;
	}

	public boolean isFullTime() {
		return fullTime;
	}

	public void setFullTime(boolean fullTime) {
		this.fullTime = fullTime;
	}
}
